

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>cup.exfile &mdash; Cup 3.2.19 documentation</title>
  

  
  
  
  

  

  
  
    

  

  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 

  
  <script src="../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../index.html" class="icon icon-home"> Cup
          

          
          </a>

          
            
            
              <div class="version">
                3.2.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../cup.html">cup package</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">Cup</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../index.html">Module code</a> &raquo;</li>
        
      <li>cup.exfile</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for cup.exfile</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: utf-8 -*</span>
<span class="c1"># Copyright: [CUP] - See LICENSE for details.</span>
<span class="c1"># Authors: Guannan Ma (@mythmgn),</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">:description:</span>
<span class="sd">    file related functions</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">copy</span>
<span class="kn">import</span> <span class="nn">shutil</span>


<span class="kn">from</span> <span class="nn">cup</span> <span class="k">import</span> <span class="n">err</span>
<span class="kn">from</span> <span class="nn">cup</span> <span class="k">import</span> <span class="n">decorators</span>
<span class="kn">from</span> <span class="nn">cup</span> <span class="k">import</span> <span class="n">platforms</span>

<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span>
    <span class="s1">&#39;LockFile&#39;</span><span class="p">,</span> <span class="s1">&#39;FILELOCK_SHARED&#39;</span><span class="p">,</span> <span class="s1">&#39;FILELOCK_EXCLUSIVE&#39;</span><span class="p">,</span>
    <span class="s1">&#39;FILELOCK_NONBLOCKING&#39;</span><span class="p">,</span> <span class="s1">&#39;FILELOCK_UNLOCK&#39;</span><span class="p">,</span> <span class="s1">&#39;mk_newnode&#39;</span><span class="p">,</span> <span class="s1">&#39;safe_rmtree&#39;</span><span class="p">,</span>
    <span class="s1">&#39;safe_delete&#39;</span>
<span class="p">]</span>


<span class="n">CANNOT_DEL_PATHLIST</span> <span class="o">=</span> <span class="p">[</span>
    <span class="s1">&#39;/&#39;</span><span class="p">,</span>
    <span class="s1">&#39;/proc&#39;</span><span class="p">,</span>
    <span class="s1">&#39;/boot&#39;</span><span class="p">,</span>
    <span class="s1">&#39;/sys&#39;</span>
<span class="p">]</span>


<span class="k">if</span> <span class="n">platforms</span><span class="o">.</span><span class="n">is_linux</span><span class="p">()</span> <span class="ow">or</span> <span class="n">platforms</span><span class="o">.</span><span class="n">is_mac</span><span class="p">():</span>
    <span class="kn">import</span> <span class="nn">fcntl</span>
    <span class="n">FILELOCK_EXCLUSIVE</span> <span class="o">=</span> <span class="n">fcntl</span><span class="o">.</span><span class="n">LOCK_EX</span>
    <span class="n">FILELOCK_SHARED</span> <span class="o">=</span> <span class="n">fcntl</span><span class="o">.</span><span class="n">LOCK_SH</span>
    <span class="n">FILELOCK_NONBLOCKING</span> <span class="o">=</span> <span class="n">fcntl</span><span class="o">.</span><span class="n">LOCK_NB</span>
    <span class="n">FILELOCK_UNLOCK</span> <span class="o">=</span> <span class="n">fcntl</span><span class="o">.</span><span class="n">LOCK_UN</span>
<span class="k">elif</span> <span class="n">platforms</span><span class="o">.</span><span class="n">is_windows</span><span class="p">():</span>
    <span class="kn">import</span> <span class="nn">msvcrt</span>

    <span class="k">def</span> <span class="nf">file_size</span><span class="p">(</span><span class="n">fobj</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;win file size&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">getsize</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">realpath</span><span class="p">(</span><span class="n">fobj</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> <span class="p">)</span>

    <span class="k">def</span> <span class="nf">win_lockfile</span><span class="p">(</span><span class="n">fobj</span><span class="p">,</span> <span class="n">blocking</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;win lock file&quot;&quot;&quot;</span>
        <span class="n">flags</span> <span class="o">=</span> <span class="n">msvcrt</span><span class="o">.</span><span class="n">LK_RLCK</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">blocking</span><span class="p">:</span>
            <span class="n">flags</span> <span class="o">=</span> <span class="n">msvcrt</span><span class="o">.</span><span class="n">LK_NBRLCK</span>
        <span class="n">msvcrt</span><span class="o">.</span><span class="n">locking</span><span class="p">(</span><span class="n">fobj</span><span class="o">.</span><span class="n">fileno</span><span class="p">(),</span> <span class="n">flags</span><span class="p">,</span> <span class="n">file_size</span><span class="p">(</span><span class="n">fobj</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">win_unlockfile</span><span class="p">(</span><span class="n">fobj</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;win unlock file&quot;&quot;&quot;</span>
        <span class="n">msvcrt</span><span class="o">.</span><span class="n">locking</span><span class="p">(</span><span class="n">fobj</span><span class="o">.</span><span class="n">fileno</span><span class="p">(),</span> <span class="n">msvcrt</span><span class="o">.</span><span class="n">LK_UNLCK</span><span class="p">,</span> <span class="n">file_size</span><span class="p">(</span><span class="n">fobj</span><span class="p">))</span>


<div class="viewcode-block" id="LockFile"><a class="viewcode-back" href="../../cup.html#cup.exfile.LockFile">[docs]</a><span class="k">class</span> <span class="nc">LockFile</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Lock file in order to prevent others from trying to lock it again</span>

<span class="sd">    Code Example:</span>
<span class="sd">    ::</span>

<span class="sd">        from cup import exfile</span>

<span class="sd">        filelock = exfile.LockFile()</span>
<span class="sd">        # xxxx do something</span>
<span class="sd">        filelock.lock(blocking=True)</span>
<span class="sd">        # xxxxx do something else</span>
<span class="sd">        filelock.unlock()</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fpath</span><span class="p">,</span> <span class="n">locktype</span><span class="o">=</span><span class="n">FILELOCK_EXCLUSIVE</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        exclusive lockfile, by default.</span>

<span class="sd">        Notice that the file CANNOT exist before you intialize a LockFile obj.</span>
<span class="sd">        Otherwise, it will raise cup.err.LockFileError</span>

<span class="sd">        Plz notice that on windows, cup only support EXCLUSIVE lock</span>

<span class="sd">        :raise:</span>
<span class="sd">            cup.err.LockFileError if we encounter errors</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_fpath</span> <span class="o">=</span> <span class="n">fpath</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_locktype</span> <span class="o">=</span> <span class="n">locktype</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_fhandle</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_fhandle</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">open</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_fpath</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">O_CREAT</span> <span class="o">|</span> <span class="n">os</span><span class="o">.</span><span class="n">O_RDWR</span>
            <span class="p">)</span>
        <span class="k">except</span> <span class="ne">IOError</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">err</span><span class="o">.</span><span class="n">LockFileError</span><span class="p">(</span><span class="n">error</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">err</span><span class="o">.</span><span class="n">LockFileError</span><span class="p">(</span><span class="n">error</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">err</span><span class="o">.</span><span class="n">LockFileError</span><span class="p">(</span>
                <span class="s1">&#39;catch unkown error type:</span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">error</span><span class="p">)</span>
            <span class="p">)</span>

    <span class="k">def</span> <span class="nf">__del__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;del the instance&quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_fhandle</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">os</span><span class="o">.</span><span class="n">close</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_fhandle</span><span class="p">)</span>
        <span class="c1"># pylint: disable=W0703</span>
        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;failed to close lockfile:</span><span class="si">{0}</span><span class="s1">, msg:</span><span class="si">{1}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_fpath</span><span class="p">,</span> <span class="n">error</span><span class="p">)</span>
            <span class="p">)</span>
            <span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>

<div class="viewcode-block" id="LockFile.lock"><a class="viewcode-back" href="../../cup.html#cup.exfile.LockFile.lock">[docs]</a>    <span class="nd">@decorators</span><span class="o">.</span><span class="n">needposix</span>
    <span class="k">def</span> <span class="nf">lock</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">blocking</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        lock the file</span>

<span class="sd">        :param blocking:</span>
<span class="sd">            If blocking is True, will block there until cup gets the lock.</span>
<span class="sd">            True by default.</span>

<span class="sd">        :return:</span>
<span class="sd">            return False if locking fails</span>

<span class="sd">        :raise Exception:</span>
<span class="sd">            raise cup.err.LockFileError if blocking is False and</span>
<span class="sd">            the lock action failed</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">platforms</span><span class="o">.</span><span class="n">is_linux</span><span class="p">():</span>
            <span class="n">flags</span> <span class="o">=</span> <span class="mh">0x1</span>
            <span class="k">if</span> <span class="n">FILELOCK_SHARED</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_locktype</span><span class="p">:</span>
                <span class="n">flags</span> <span class="o">=</span> <span class="n">FILELOCK_SHARED</span>
            <span class="k">elif</span> <span class="n">FILELOCK_EXCLUSIVE</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_locktype</span><span class="p">:</span>
                <span class="n">flags</span> <span class="o">=</span> <span class="n">FILELOCK_EXCLUSIVE</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">err</span><span class="o">.</span><span class="n">LockFileError</span><span class="p">(</span><span class="s1">&#39;does not support this lock type&#39;</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">blocking</span><span class="p">:</span>
                <span class="n">flags</span> <span class="o">|=</span> <span class="n">FILELOCK_NONBLOCKING</span>
            <span class="n">ret</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">ret</span> <span class="o">=</span> <span class="n">fcntl</span><span class="o">.</span><span class="n">flock</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_fhandle</span><span class="p">,</span> <span class="n">flags</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">IOError</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">err</span><span class="o">.</span><span class="n">LockFileError</span><span class="p">(</span><span class="n">error</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">err</span><span class="o">.</span><span class="n">LockFileError</span><span class="p">(</span><span class="n">error</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">ret</span>
        <span class="k">elif</span> <span class="n">platforms</span><span class="o">.</span><span class="n">is_windows</span><span class="p">():</span>
            <span class="n">win_lockfile</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_fhandle</span><span class="p">,</span> <span class="n">blocking</span><span class="p">)</span></div>

<div class="viewcode-block" id="LockFile.unlock"><a class="viewcode-back" href="../../cup.html#cup.exfile.LockFile.unlock">[docs]</a>    <span class="k">def</span> <span class="nf">unlock</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;unlock the locked file&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">platforms</span><span class="o">.</span><span class="n">is_linux</span><span class="p">():</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">fcntl</span><span class="o">.</span><span class="n">flock</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_fhandle</span><span class="p">,</span> <span class="n">FILELOCK_UNLOCK</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">err</span><span class="o">.</span><span class="n">LockFileError</span><span class="p">(</span><span class="n">error</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">platforms</span><span class="o">.</span><span class="n">is_windows</span><span class="p">():</span>
            <span class="n">win_unlockfile</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_fhandle</span><span class="p">)</span></div>

<div class="viewcode-block" id="LockFile.filepath"><a class="viewcode-back" href="../../cup.html#cup.exfile.LockFile.filepath">[docs]</a>    <span class="k">def</span> <span class="nf">filepath</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        return filepath</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_fpath</span></div></div>



<div class="viewcode-block" id="mk_newnode"><a class="viewcode-back" href="../../cup.html#cup.exfile.mk_newnode">[docs]</a><span class="k">def</span> <span class="nf">mk_newnode</span><span class="p">(</span><span class="n">abspath</span><span class="p">,</span> <span class="n">check_exsistence</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    make new file node for abspath</span>

<span class="sd">    :param abspath:</span>
<span class="sd">        plz use absolute path. Not relative path</span>
<span class="sd">    :param check_exsistence:</span>
<span class="sd">        if True, will check if the abspath existence (</span>
<span class="sd">        raise IOError if abspath exists)</span>
<span class="sd">    :raise Exception:</span>
<span class="sd">        IOError</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">check_exsistence</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">abspath</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">IOError</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{0}</span><span class="s1"> already exists&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">abspath</span><span class="p">))</span>
    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">abspath</span><span class="p">,</span> <span class="s1">&#39;w+&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">_</span><span class="p">:</span>
        <span class="k">pass</span></div>


<div class="viewcode-block" id="safe_rmtree"><a class="viewcode-back" href="../../cup.html#cup.exfile.safe_rmtree">[docs]</a><span class="k">def</span> <span class="nf">safe_rmtree</span><span class="p">(</span><span class="n">abspath</span><span class="p">,</span> <span class="n">not_del_list</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    the function will safely remove files/dirs of abspath with not_del_list</span>
<span class="sd">    excluded</span>

<span class="sd">    :param abspath:</span>
<span class="sd">        pass in absolute path</span>
<span class="sd">    :param not_del_list:</span>
<span class="sd">        cannot del path list</span>

<span class="sd">    :raise Exception:</span>
<span class="sd">        ValueError, if abspath is in exfile.CANNOT_DEL_PATHLIST or not_del_list</span>
<span class="sd">        IOError, if cup encounters any problem</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">normpath</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">normpath</span><span class="p">(</span><span class="n">abspath</span><span class="p">)</span>
    <span class="n">tmplist</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">CANNOT_DEL_PATHLIST</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">not_del_list</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">tmplist</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">not_del_list</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">abspath</span> <span class="ow">in</span> <span class="n">tmplist</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
            <span class="s1">&#39;cannot delete path in </span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">tmplist</span><span class="p">)</span>
        <span class="p">)</span>
    <span class="n">shutil</span><span class="o">.</span><span class="n">rmtree</span><span class="p">(</span><span class="n">normpath</span><span class="p">)</span></div>


<div class="viewcode-block" id="safe_delete"><a class="viewcode-back" href="../../cup.html#cup.exfile.safe_delete">[docs]</a><span class="k">def</span> <span class="nf">safe_delete</span><span class="p">(</span><span class="n">abspath</span><span class="p">,</span> <span class="n">not_del_list</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    the function will safely delete file/object of abspath.</span>

<span class="sd">    If the abspath in not_del_list, safe delete will raise ValueError</span>

<span class="sd">    :param abspath:</span>
<span class="sd">        pass in absolute path</span>
<span class="sd">    :param not_del_list:</span>
<span class="sd">        cannot del path list</span>

<span class="sd">    :raise Exception:</span>
<span class="sd">        ValueError, if abspath is in exfile.CANNOT_DEL_PATHLIST or not_del_list</span>
<span class="sd">        IOError, if cup encounters any problem</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">normpath</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">normpath</span><span class="p">(</span><span class="n">abspath</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">normpath</span> <span class="ow">in</span> <span class="n">not_del_list</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;cannot delete files in </span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">not_del_list</span><span class="p">))</span>
    <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">abspath</span><span class="p">):</span>
        <span class="n">safe_rmtree</span><span class="p">(</span><span class="n">abspath</span><span class="p">,</span> <span class="n">not_del_list</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">os</span><span class="o">.</span><span class="n">unlink</span><span class="p">(</span><span class="n">abspath</span><span class="p">)</span></div>

<span class="c1"># vi:set tw=0 ts=4 sw=4 nowrap fdm=indent</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, CUP-DEV.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../',
            VERSION:'3.2.19',
            LANGUAGE:'en',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../_static/doctools.js"></script>
      <script type="text/javascript" src="../../_static/language_data.js"></script>

  

  <script type="text/javascript" src="../../_static/js/theme.js"></script>

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>